package com.lw.common.customview

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import com.lw.common.utils.DateUtil.getNextDay
import com.lw.common.utils.DateUtil.nowDateStr
import com.lw.common.utils.DateUtil.strToDate
import com.lw.common.utils.UIModeUtil
import com.lxj.xpopup.XPopup
import com.lxj.xpopupext.listener.TimePickerListener
import com.lxj.xpopupext.popup.TimePickerPopup
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date

/**
 * @description: 日期选择控件封装
 * @author: yuan
 * @date: 2024/12/16
 */
object TimePickerPop {
    /**
     * 只显示年
     */
    fun showY(context: Context, mListener: OnItemClickListener) {
        val nowDate = nowDateStr
        val date = strToDate(getNextDay(nowDate))
        val calendar = Calendar.getInstance()
        calendar.time = date!!
        val popup =
            TimePickerPopup(context).setMode(TimePickerPopup.Mode.Y).setDefaultDate(calendar).setTimePickerListener(object : TimePickerListener {
                override fun onTimeChanged(date: Date) {
                    //时间改变
                }

                override fun onTimeConfirm(date: Date, view: View) {
                    //点击确认时间
                    @SuppressLint("SimpleDateFormat")
                    val simpleDateFormat = SimpleDateFormat("yyyy")
                    val dateStr = simpleDateFormat.format(date)
                    mListener.onItemClick(dateStr)
                }

                override fun onCancel() {
                }
            })

        val isNightMode: Boolean = UIModeUtil.isNightMode(context)
        XPopup.Builder(context).isDarkTheme(isNightMode).asCustom(popup).show()
    }

    /**
     * 只显示年月
     */
    fun showYM(context: Context, mListener: OnItemClickListener) {
        val nowDate = nowDateStr
        val date = strToDate(getNextDay(nowDate))
        val calendar = Calendar.getInstance()
        calendar.time = date!!
        val popup =
            TimePickerPopup(context).setMode(TimePickerPopup.Mode.YM).setDefaultDate(calendar).setTimePickerListener(object : TimePickerListener {
                override fun onTimeChanged(date: Date) {
                    //时间改变
                }

                override fun onTimeConfirm(date: Date, view: View) {
                    //点击确认时间
                    @SuppressLint("SimpleDateFormat") val simpleDateFormat = SimpleDateFormat("yyyy-MM")
                    val dateStr = simpleDateFormat.format(date)
                    mListener.onItemClick(dateStr)
                }

                override fun onCancel() {
                }
            })

        val isNightMode: Boolean = UIModeUtil.isNightMode(context)
        XPopup.Builder(context).isDarkTheme(isNightMode).asCustom(popup).show()
    }

    /**
     * 只显示年月日
     */
    fun showYMD(context: Context, mListener: OnItemClickListener) {
        val popup = TimePickerPopup(context).setMode(TimePickerPopup.Mode.YMD).setTimePickerListener(object : TimePickerListener {
            override fun onTimeChanged(date: Date) {
                //时间改变
            }

            override fun onTimeConfirm(date: Date, view: View) {
                //点击确认时间
                @SuppressLint("SimpleDateFormat") val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd")
                val dateStr = simpleDateFormat.format(date)
                mListener.onItemClick(dateStr)
            }

            override fun onCancel() {
            }
        })

        val isNightMode: Boolean = UIModeUtil.isNightMode(context)
        XPopup.Builder(context).isDarkTheme(isNightMode).asCustom(popup).show()
    }

    /**
     * 默认显示年、月、日、时、分、秒
     */
    fun show(context: Context, mListener: OnItemClickListener) {
        val nowDate: String = nowDateStr
        val date = strToDate(getNextDay(nowDate))
        val calendar = Calendar.getInstance()
        calendar.time = date!!
        val popup =
            TimePickerPopup(context).setMode(TimePickerPopup.Mode.YMDHMS).setDefaultDate(calendar).setTimePickerListener(object : TimePickerListener {
                override fun onTimeChanged(date: Date) {
                    //时间改变
                }

                override fun onTimeConfirm(date: Date, view: View) {
                    //点击确认时间
                    @SuppressLint("SimpleDateFormat") val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                    val dateStr = simpleDateFormat.format(date)
                    mListener.onItemClick(dateStr)
                }

                override fun onCancel() {
                }
            })
        val isNightMode: Boolean = UIModeUtil.isNightMode(context)
        XPopup.Builder(context).isDarkTheme(isNightMode).asCustom(popup).show()
    }

    interface OnItemClickListener {
        //在初始化时获得事件
        fun onItemClick(date: String?)
    }
}
